package com.ego.item.service.impl;

import com.ego.commons.pojo.TbItemDetails;
import com.ego.dubbo.service.TbItemCatDubboService;
import com.ego.dubbo.service.TbItemDescDubboService;
import com.ego.dubbo.service.TbItemDubboService;
import com.ego.dubbo.service.TbItemParamItemDubboService;
import com.ego.item.pojo.CategoryNode;
import com.ego.item.pojo.ItemCategoryNav;
import com.ego.item.service.ItemService;
import com.ego.pojo.TbItem;
import com.ego.pojo.TbItemCat;
import com.ego.pojo.TbItemDesc;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ghp
 */
@Service
public class ItemServiceImpl implements ItemService {
    @Reference
    private TbItemCatDubboService tbItemCatDubboService;
    @Reference
    private TbItemDubboService tbItemDubboService;
    @Reference
    private TbItemDescDubboService tbItemDescDubboService;
    @Reference
    private TbItemParamItemDubboService tbItemParamItemDubboService;
    @Override
    @Cacheable(cacheNames = "com.ego.item",key = "'showItemCat'")
    public ItemCategoryNav showItemCat() {
        System.out.println("执行方法");
        ItemCategoryNav itemCategoryNav = new ItemCategoryNav();
        itemCategoryNav.setData(getAllItemCat(0L));
        return itemCategoryNav;
    }

    /**
     *显示商品详情
     * @param id
     * @return
     */
    @Cacheable(cacheNames = "com.ego.item",key = "'details:'+#id")
    @Override
    public TbItemDetails showItem(Long id) {
        System.out.println("执行了方法");
        TbItem tbItem = this.tbItemDubboService.selectById(id);
        TbItemDetails details = new TbItemDetails();
        details.setId(tbItem.getId());
        details.setPrice(tbItem.getPrice());
        details.setTitle(tbItem.getTitle());
        details.setSellPoint(tbItem.getSellPoint());
        String image = tbItem.getImage();
        details.setImages(image !=null && !image.equals("")?image.split(","):new String[1]);
        return details;
    }

    @Override
    public String showItemDesc(Long itemId) {
        TbItemDesc tbItemDesc = this.tbItemDescDubboService.selectById(itemId);
        return tbItemDesc.getItemDesc();
    }

    @Override
    public String showItemParam(Long itemId) {

        return null;
    }

    /**
     * 递归调用返回数据
     * @param parentId
     * @return
     */
    private List<Object> getAllItemCat(Long parentId){
        List<TbItemCat> list = tbItemCatDubboService.selectByPid(parentId);
        List<Object> listResult = new ArrayList<>();
        // 一个cat对应一个菜单项目。前两层类型都是CategoryNode类型，第三层是String
        for(TbItemCat cat : list){
            // 说明是第一层或第二层。
            if(cat.getIsParent()){
                CategoryNode node = new CategoryNode();
                node.setU("/products/"+cat.getId()+".html");
                node.setN("<a href='/products/"+cat.getId()+".html'>"+cat.getName()+"</a>");
                //递归调用
                node.setI(getAllItemCat(cat.getId()));
                listResult.add(node);
            }else{
                listResult.add("/products/"+cat.getId()+".html|"+cat.getName());
            }

        }
        return listResult;
    }
}
